package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierMapper;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierDao;
    @Autowired
    private LogService logService;

    /**
     * 分页查询供应商
     *
     * @param page         当前页数
     * @param rows         每页显示的记录数
     * @param supplierName 供应商名字
     * @return
     */
    @Override
    public Map<String, Object> supplierList(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        //从那条数据开始
        int offSet = (page - 1) * rows;
        List<Supplier> supplierList = supplierDao.supplierList(offSet, rows, supplierName);
        if (supplierList != null && supplierList.size() > 0) {
            map.put("rows", supplierList);
            map.put("total", supplierList.size());
        }
        return map;
    }

    /**
     * 添加或修改供应商信息
     *
     * @param supplier
     * @return
     */
    @Override
    public ServiceVO supplierSave(Supplier supplier) {
        if (supplier.getSupplierId() == null) {
            //根据供应商名字进行查询
            Supplier exSupplier = supplierDao.findSupplierByName(supplier.getSupplierName());
            if (exSupplier != null) {
                return new ServiceVO(ErrorCode.ACCOUNT_EXIST_CODE, ErrorCode.ACCOUNT_EXIST_MESS);
            }
            supplierDao.addSupplier(supplier);
            logService.save(new Log(Log.INSERT_ACTION, "添加用户" + supplier.getSupplierName()));
        } else {
            supplierDao.updateSupplier(supplier);
            logService.save(new Log(Log.UPDATE_ACTION, "修改用户" + supplier.getSupplierName()));

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除供应商（支持批量删除）
     *
     * @param ids
     * @return
     */
    @Override
    public ServiceVO supplierDelete(String ids) {
        String[] split = StringUtils.split(ids, ",");
        if (split != null && split.length > 0) {
            Arrays.stream(split).forEach(id -> {
                //根据id进行查找 供应商信息
                Supplier supplier = supplierDao.getSupplierById(id);
                if (supplier == null) {
                    logService.save(new Log(Log.DELETE_ACTION, "没有对应的id" + id));
                     return;
                } else {
                    supplierDao.deleteSupplierById(id);
                    logService.save(new Log(Log.DELETE_ACTION, "删除成功" + id));
                }
            });
        }else if (ids!=null){
            supplierDao.deleteSupplierById(ids);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


}
